package br.edu.univas.si.lab3.bankmanager.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;

import br.edu.univas.si.lab3.bankmanager.listeners.BotoesListener;
import br.edu.univas.si.lab3.bankmanager.model.DAOException;


public class PanelButtons extends JPanel{
	private static final long serialVersionUID = 1L;

	private JButton okButton;
	private JButton cancelButton;
	
	private ArrayList<BotoesListener> listeners = new ArrayList<BotoesListener>();
	
	public PanelButtons() {
		initialize();
	}
	
	private void initialize() {
		add(getOkButton());
		add(getCancelButton());
	}
	
	private JButton getOkButton() {
		if(okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
			okButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					for (BotoesListener listener : listeners) {
						try {
							listener.okPerformed();
						} catch (DAOException e) {
							e.printStackTrace();
						}
					}
				}
			});
		}
		return okButton;
	}

	private JButton getCancelButton() {
		if(cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancelar");
			cancelButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					for (BotoesListener listener : listeners) {
						listener.cancelarPerformed();
					}
				}
			});
		}
		return cancelButton;
	}
	
	public void addBotoesListener(BotoesListener listener) {
		listeners.add(listener);
	}

}
